package zj.it.bhne.huanxin.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.widget.EaseTitleBar;
import com.hyphenate.exceptions.HyphenateException;

import zj.it.bhne.huanxin.R;
import zj.it.bhne.huanxin.myuntils.HuanApplication;

public class AddFriendActivity extends AppCompatActivity implements View.OnClickListener {

    private EaseTitleBar title_bar;
    private EditText add_name;
    private EditText add_reason;
    private Button add_sure;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_friend);
        context = this;
        initView();
    }

    private void initView() {
        title_bar = findViewById(R.id.title_bar);
        title_bar.setTitle("添加好友");
        add_name = findViewById(R.id.add_name);
        add_name.setOnClickListener(this);
        add_reason = findViewById(R.id.add_reason);
        add_reason.setOnClickListener(this);
        add_sure = findViewById(R.id.add_sure);
        add_sure.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_sure:
                //参数为要添加的好友的username和添加理由
                String name = add_name.getText().toString().trim();
                String reason = add_reason.getText().toString().trim();
                try {
                    EMClient.getInstance().contactManager().addContact(name, reason);
                    HuanApplication.setShortToast(context,"添加成功");
                } catch (HyphenateException e) {
                    HuanApplication.setShortToast(context,"添加失败");
                    e.printStackTrace();
                }finally {
                    startActivity(new Intent(context,HomeActivity.class));
                }
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

}
